package com.wave.gate.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.wave.gate.vo.LinuxSystemIp;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Objects;

/**
 * @author yanZhiHang
 * @date 2023/2/2 11:59
 */
@Slf4j
public class GetLocalHost {

    private static final String LINUX_CMD_IP = "/sbin/ifconfig eth0";

    public static void main(String[] args) throws Exception {
        InetAddress addr = InetAddress.getLocalHost();
        log.info("Local HostAddress: "+addr.getHostAddress());
        String hostname = addr.getHostName();
        log.info("Local host name: "+hostname);
        log.info("本机ip：" + getIpDetails());
    }

    public static String getIpAddress() {
        try {
            //从网卡中获取IP
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip;
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                //用于排除回送接口,非虚拟网卡,未在使用中的网络接口
                if (!netInterface.isLoopback() && !netInterface.isVirtual() && netInterface.isUp()) {
                    //返回和网络接口绑定的所有IP地址
                    Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        ip = addresses.nextElement();
                        if (ip instanceof Inet4Address) {
                            return ip.getHostAddress();
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("IP地址获取失败:[{}]",e.getMessage(),e);
        }
        return "";
    }

    public static LinuxSystemIp getIpDetails() {
        LinuxSystemIp settingSystemEntity = new LinuxSystemIp();
        try {
            Process process = Runtime.getRuntime().exec(LINUX_CMD_IP);
            InputStreamReader inputStream = new InputStreamReader(process.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(inputStream);
            String line;
            List<String> details = new ArrayList<>();
            while (Objects.nonNull(line = bufferedReader.readLine())) {
                details.add(line.trim());
            }
            if (CollectionUtil.isNotEmpty(details)) {
                for (String str : details) {
                    if (str.startsWith("inet")) {
                        String[] strings = str.split(" ");
                        if (StrUtil.isNotBlank(strings[1])) {
                            settingSystemEntity.setIpv4Inet(strings[1]);
                        }
                        if (StrUtil.isNotBlank(strings[4])) {
                            settingSystemEntity.setIpv4netmask(strings[4]);
                        }
                        if (StrUtil.isNotBlank(strings[7])) {
                            settingSystemEntity.setIpv4Broadcast(strings[7]);
                        }
                        break;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return settingSystemEntity;
    }
}


